package com.ys.service.Impl;

import com.ys.mapper.*;
import com.ys.pojo.*;
import com.ys.service.DrugsService;
import com.ys.vo.AllocationVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service("drugsService")
@Transactional
public class DrugsServiceImpl implements DrugsService {
    @Resource
    private DrugsMapper drugsMapper;
    @Resource
    AllocationRecordMapper allocationRecordMapper;
    @Override
    public List<Drugs> select(String id, String name1,String name2,String name3,String name4) {
        return drugsMapper.select(id,name1,name2,name3,name4);
    }
    @Override
    public Drugs select(String id) {
        return drugsMapper.selectsssss(id);
    }

    @Override
    public boolean update(Drugs drugs) {
        return drugsMapper.update(drugs);
    }

    @Override
    public boolean add(Drugs drugs) {
        List<Drugs> list = drugsMapper.findAll();
        for (Drugs li:list){
            if (drugs.getDrugsid().equals(li.getDrugsid())){
                return false;
            }
        }
        return drugsMapper.add(drugs);
    }
    @Override
    public boolean del(int id) {
        return drugsMapper.del(id);
    }

    @Override
    public boolean updateku(int id, String ids) {
        return drugsMapper.updatekucun(id,ids);
    }


//    @Override
//    public void updNumber(AllocationVo allocationVo) {
//        Drugs drugs = searchDrugsId(allocationVo.getDrugsId());
//        drugsMapper.updNumber(allocationVo.getRecall(),-allocationVo.getNumber(),allocationVo.getDrugsId(),drugs.getDrugsName());
//        drugsMapper.updNumber(allocationVo.getCallIn(),allocationVo.getNumber(),allocationVo.getDrugsId(),drugs.getDrugsName());
//    }

    @Override
    public void updNumber(AllocationVo allocationVo) {
        Drugs drugs = searchDrugsId(allocationVo.getDrugsId());
        drugsMapper.updNumber(allocationVo.getRecall(),-allocationVo.getNumber(),allocationVo.getDrugsId(),drugs.getDrugsName());
        drugsMapper.updNumber(allocationVo.getCallIn(),allocationVo.getNumber(),allocationVo.getDrugsId(),drugs.getDrugsName());
        AllocationRecord allocationRecord = new AllocationRecord(allocationVo.getRecall(),allocationVo.getCallIn(),allocationVo.getDrugsId(),allocationVo.getNumber(), LocalDate.now());
        allocationRecordMapper.add(allocationRecord);
    }


    @Override
    @Transactional(readOnly = true)
    public List<Drugs> searchAll() {
        return drugsMapper.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Drugs> searchWareId(Integer wid) {
        return drugsMapper.findWarId(wid);
    }

    @Override
    @Transactional(readOnly = true)
    public int searchExist(Integer wId, Integer drugsId) {
        Drugs drugs = searchDrugsId(drugsId);
        return drugsMapper.findExist(wId,drugs.getDrugsName());
    }

    @Override
    @Transactional(readOnly = true)
    public Drugs searchDrugsId(Integer drugsId) {
        return drugsMapper.findDrugId(drugsId);
    }

    @Override
    public int searchDrugsIdMax() {
        return drugsMapper.findDrugsIdMax();
    }

    @Override
    public boolean addNew(Drugs drugs, AllocationVo allocationVo) {
        drugsMapper.updAddNumber(allocationVo.getDrugsId(),allocationVo.getNumber());
        return drugsMapper.addNew(drugs);
    }

    /**
     * 根据药品名称查询药品
     * @param drugsName  药品名称
     * @return
     */
    @Override
    public List<Drugs> searchDrugsName(String drugsName) {
        return drugsMapper.findDrugsName(drugsName);
    }

    @Resource
    private InventoryMapper inventoryMapper;
    @Resource
    private InventoryRecordMapper inventoryRecordMapper;
    /**
     * 更正库存数量
     * @param drugs
     * @return
     */
    @Override
    public boolean updChaNum(Drugs[] drugs,String comment) {
        //添加盘点单
        Integer chaNum = 0;     //盘点表要用到的总盘点总差异
        for (int i = 0; i <drugs.length ; i++) {
            chaNum+=Math.abs(drugs[i].getChaNum());
        }
        inventoryMapper.add(new Inventory(LocalDate.now(),chaNum,comment));     //添加盘点单完成

        //添加盘点详情单
        //1.先获取盘点最新添加的一条
        int newInvent = inventoryMapper.findMax();
        //2.循环添加信息
        for (int i = 0; i <drugs.length ; i++) {
            drugsMapper.updChaNum(drugs[i]);        //批量修正数量
            //3.定义声明一个盘点详情对象
            int panNum = 0;             //定义实际数量
            panNum=drugs[i].getNumber()+drugs[i].getChaNum();
            InventoryRecord inRecord = new InventoryRecord(drugs[i].getDrugsid(),drugs[i].getDrugsName(),drugs[i].getPrice(),drugs[i].getWname(),drugs[i].getNumber(),panNum,newInvent);
            inventoryRecordMapper.add(inRecord);
        }
        return true;
    }


    @Override
    public List<Drugs> searchBitId(Integer bitId) {
        return drugsMapper.findBitId(bitId);
    }
}
